package com.zhang.communication.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zhang.communication.entity.ChatFriends;
import com.zhang.communication.mapper.ChatFriendsMapper;
import com.zhang.communication.service.IChatFriendsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.communication.vo.UserChatVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 聊天列表 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2024-06-12
 */
@Service
public class ChatFriendsServiceImpl extends ServiceImpl<ChatFriendsMapper, ChatFriends> implements IChatFriendsService {

    @Override
    public List<UserChatVO> getOpenChatLists() {
        return baseMapper.getOpenChatLists(StpUtil.getLoginIdAsString());
    }

    @Override
    public void createChatListItem(ChatFriends chatList) {
        LambdaQueryWrapper<ChatFriends> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatFriends::getUserId, chatList.getUserId()).eq(ChatFriends::getFriendId, chatList.getFriendId());
        ChatFriends one = getOne(wrapper);
        if (one == null) {
            save(chatList);
        }
    }


    @Override
    public boolean inspectIsFriend(String loginIdAsString, String friendId) {
        LambdaQueryWrapper<ChatFriends> wrapper = new LambdaQueryWrapper<>();
        // 使用or连接两个条件，确保只要满足其中一个即可
        wrapper.eq(ChatFriends::getId, loginIdAsString).eq(ChatFriends::getFriendId, friendId)
                .or()
                .eq(ChatFriends::getId, friendId).eq(ChatFriends::getFriendId, loginIdAsString);
        return getOne(wrapper) != null;
    }
}
